AVPlayer
AVPlayer provides audio and video playback capabilities, including playback control, rate control, looping, playback state observation, and media metadata loading.
You set a media source using setSource() (local file or remote URL), then start playback using play().
Getting Started
API Reference
Properties
volume: number
Controls the playback volume.
Range: 0.0 (muted) to 1.0 (maximum).
duration: DurationInSeconds
The total duration of the media in seconds.
This value is 0 until the media has finished loading.
currentTime: DurationInSeconds
The current playback position in seconds. You can assign a value to seek to a specific time.
rate: number
The current playback rate.
1.0= normal speed< 1.0= slower playback> 1.0= faster playback
This property reflects the actual playback speed while playing.
defaultRate: number
The default playback rate used when playback starts.
- Used when calling
play()without specifyingatRate - Changing
defaultRatedoes not immediately affect an ongoing playback - Primarily intended to control the rate for the next playback start
Typical use cases:
- The user selects a preferred playback speed before pressing play
- Playback automatically starts at that speed next time
play()is called
timeControlStatus: TimeControlStatus
Indicates the current playback state:
pausedPlayback is paused or has not startedwaitingToPlayAtSpecifiedRateWaiting for playback conditions (e.g. buffering, network)playingPlayback is in progress
numberOfLoops: number
Controls how many times the media will loop:
0: no looping- positive value: loop a specific number of times
- negative value: loop indefinitely
Methods
setSource(filePathOrURL: string): boolean
Sets the media source for playback.
Supports:
- Local file paths
- Remote URLs
Returns:
trueif the source was set successfullyfalseif it failed
play(atRate?: number): boolean
Starts playback of the current media.
Playback rate resolution order:
- If
atRateis provided, playback starts at that rate - Otherwise,
defaultRateis used
During playback, you can still modify rate dynamically.
Returns:
trueif playback started successfullyfalseotherwise
pause()
Pauses playback.
stop()
Stops playback and resets the position to the beginning.
dispose()
Releases all player resources and removes internal observers. Must be called when the player is no longer needed to avoid resource leaks.
loadMetadata(): Promise<AVMetadataItem[] | null>
Loads the full metadata of the current media.
Returns:
- An array of
AVMetadataItem nullif no source is set or metadata is unavailable
loadCommonMetadata(): Promise<AVMetadataItem[] | null>
Loads the common metadata of the current media.
Common metadata provides format-independent commonKey values, typically used for title, artist, album, etc.
Callbacks
onReadyToPlay?: () => void
Called when the media is ready for playback.
onTimeControlStatusChanged?: (status: TimeControlStatus) => void
Called whenever the playback state changes, such as:
- waiting → playing
- playing → paused
onEnded?: () => void
Called when playback finishes.
onError?: (message: string) => void
Called when a playback error occurs. Receives a descriptive error message.
Audio Session Notes
AVPlayer relies on the system’s shared audio session.
You should configure and activate it before playback.
Handling interruptions (e.g. phone calls):
Common Usage Examples
Play Using the Default Rate
Start Playback at a Specific Rate
Loop Playback
Read Common Metadata
Best Practices
-
Differentiate
defaultRatevsratedefaultRateaffects how playback startsratereflects or controls the current playback speed
-
Always Release Resources
- Call
dispose()when playback ends or the player is no longer needed
- Call
-
Observe Playback State
- Use
onTimeControlStatusChangedto update loading or playing UI states
- Use
-
Configure Audio Session Before Playing
- Prevent unexpected background, mute, or mixing behavior
-
Metadata Timing
- Reading metadata after
onReadyToPlayis more reliable
- Reading metadata after
